package com.daocao.ds.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daocao.ds.MainActivity;
import com.daocao.ds.R;
import com.daocao.ds.activity.LoginByPassActivity;
import com.daocao.ds.activity.ManagerReportActivity;
import com.daocao.ds.activity.MyMessageActivity;
import com.daocao.ds.activity.SettingActivity;
import com.daocao.ds.activity.TransactionRecordActivity;
import com.daocao.ds.activity.WechatChatActivity;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.model.UserModel;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.event.QuaSetSuccessEvent;
import com.daocao.ds.event.UserEvent;
import com.daocao.ds.event.UserNoDataEvent;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xsooy-pc on 18-11-16.
 */

public class MyFragment extends BaseFragment {
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.btn_msg)
    ImageView btnMsg;
    @BindView(R.id.btn_my_setting)
    ImageView btnMySetting;
    @BindView(R.id.btn_month_profit)
    TextView btnMonthProfit;
    @BindView(R.id.txt_month_profit)
    TextView txtMonthProfit;
    @BindView(R.id.txt_month_return_money)
    TextView txtMonthReturnMoney;
    @BindView(R.id.txt_month_rate)
    TextView txtMonthRate;
    @BindView(R.id.txt_total_money)
    TextView txtTotalMoney;
    @BindView(R.id.txt_balance)
    TextView txtBalance;
    @BindView(R.id.txt_bond)
    TextView txtBond;
    @BindView(R.id.txt_total_loan)
    TextView txtTotalLoan;
    @BindView(R.id.txt_total_profit)
    TextView txtTotalProfit;
    @BindView(R.id.txt_total_rate)
    TextView txtTotalRate;
    @BindView(R.id.btn_baobiao)
    FrameLayout btnBaobiao;
    @BindView(R.id.btn_return_detail)
    FrameLayout btnReturnDetail;
    @BindView(R.id.btn_wechat)
    FrameLayout btnWechat;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                ((MainActivity) getActivity()).getUser();
            }
        });
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserEvent event) {
        setUserdata(event.item);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserNoDataEvent event) {
        initUserData();
    }
//    public void getUser() {
//        if (!Network.checkNetwork(getContext())) {
//            ToastUtil.makeText(getContext(), "网络连接失败").show();
//            return;
//        }
//        refresh.setRefreshing(true);
//        Api.api_service.getUser().compose(RxLifeUtil.NocheckOn(this)).
//                doFinally(() -> {
//                    try {
//                        refresh.setRefreshing(false);
//                    } catch (NullPointerException e) {
//
//                    }
//                }).
//                subscribe(result -> {
//                    if (result.code.equals("00")) {
//                        setUserdata(result.datas);
//                    } else {
//                        initUserData();
//                    }
//                });
//    }

    private void setUserdata(UserModel.DatasBean item) {
        UserInfoGlobal.saveUserData(item);
        txtMonthProfit.setText(item.month_profit);
        txtMonthReturnMoney.setText(item.loans_money);
        txtMonthRate.setText(item.rate + "%");
        txtTotalMoney.setText(item.total);
        txtBalance.setText(item.balances);
        txtBond.setText(item.credit_money);
        txtTotalLoan.setText(item.total_loans);
        txtTotalProfit.setText(item.total_profit);
        txtTotalRate.setText(item.total_rate + "%");

        txtName.setText(item.loaner.mobile);
        Glide.with(getContext()).load(item.loaner.header).error(R.mipmap.img_my_icon).into(imgIcon);
    }

    private void initUserData() {
        txtMonthProfit.setText("");
        txtMonthReturnMoney.setText("");
        txtMonthRate.setText("");
        txtTotalMoney.setText("");
        txtBalance.setText("");
        txtBond.setText("");
        txtTotalLoan.setText("");
        txtTotalProfit.setText("");
        txtTotalRate.setText("");

        txtName.setText("");
        Glide.with(getContext()).load(R.mipmap.img_my_icon).into(imgIcon);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @OnClick({ R.id.img_icon, R.id.btn_msg, R.id.btn_my_setting, R.id.btn_month_profit, R.id.btn_baobiao, R.id.btn_return_detail, R.id.btn_wechat})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.img_icon:
                break;
            case R.id.btn_msg:
//                if (!UserInfoGlobal.checkLogin()){
//                    Util.startActivity(getContext(), LoginByPassActivity.class);
//                    return;
//                }
                Util.startActivity(getContext(), MyMessageActivity.class);
                break;
            case R.id.btn_my_setting:
                Util.startActivity(getContext(), SettingActivity.class);
                break;
            case R.id.btn_month_profit:
                Util.startActivity(getContext(), ManagerReportActivity.class);
                break;
            case R.id.btn_baobiao:
//                if (!UserInfoGlobal.checkLogin()){
//                    Util.startActivity(getContext(), LoginByPassActivity.class);
//                    return;
//                }
                Util.startActivity(getContext(), ManagerReportActivity.class);
                break;
            case R.id.btn_return_detail:
//                if (!UserInfoGlobal.checkLogin()){
//                    Util.startActivity(getContext(), LoginByPassActivity.class);
//                    return;
//                }
                Util.startActivity(getContext(), TransactionRecordActivity.class);
                break;
            case R.id.btn_wechat:
                Util.startActivity(getContext(), WechatChatActivity.class);
                break;
        }
    }
}
